import { 
  HomeOutlined,
  CloudSyncOutlined,
  EnvironmentOutlined,
  ClusterOutlined,
  MonitorOutlined,
  AlertOutlined,
  SettingOutlined,
  UnlockOutlined
} from '@ant-design/icons';

const fullMenuTree = [
  {
    key: '010000',
    icon: <HomeOutlined />,
    title: '首页',
    label: '首页',
    route: '/map',
  },
  {
    key: '900000',
    icon: <HomeOutlined />,
    title: '风险监控大屏',
    label: '风险监控大屏',
    route: '/riskMonitor',
  },
  {
    key: '020000',
    icon: <CloudSyncOutlined />,
    label: '天气管理',
    children: [
      {
        key: '020100',
        // icon: menuIconDot,
        title: '天气查询',
        label: '天气查询',
        route: '/queryweather',
      },
    ],
  },
  {
    key: '050000',
    icon: <EnvironmentOutlined />,
    label: '风险区域管理',
    children: [
      {
        key: '050100',
        // icon: menuIconDot,
        title: `风险区域`,
        label: `风险区域`,
        route: '/riskarea',
      },
    ],
  },
  {
    key: '100000',
    icon: <ClusterOutlined />,
    label: '风险等级管理',
    children: [
      {
        key: '100100',
        // icon: menuIconDot,
        title: `风险等级`,
        label: `风险等级`,
        route: '/risklevel',
      },
    ],
  },
  {
    key: '210000',
    icon: <MonitorOutlined />,
    label: '风险查询',
    children: [
      {
        key: '210100',
        // icon: menuIconDot,
        title: `标的区域`,
        label: `标的区域`,
        route: '/markArea',
      },
      {
        key: '210200',
        // icon: menuIconDot,
        title: `风堪定位`,
        label: `风堪定位`,
        route: '/riskPosition',
      },
      // {
      //   key: '210300',
      //   // icon: menuIconDot,
      //   title: `实时风险`,
      //   label: `实时风险`,
      //   route: '/realtimerisk',
      // },
    ],
  },
  {
    key: '280000',
    icon: <AlertOutlined />,
    label: '应急管理',
    children: [
      {
        key: '280100',
        // icon: menuIconDot,
        title: '应急事项',
        label: '应急事项',
        route: '/emergencyevent',
      },
      {
        key: '280200',
        // icon: menuIconDot,
        title: '应急通知',
        label: '应急通知',
        route: '/emergencyinform',
      },
      {
        key: '430100',
        // icon: menuIconDot,
        title: '应急通知模板',
        label: '应急通知模板',
        route: '/informtemplate',
      },
      {
        key: '430200',
        // icon: menuIconDot,
        title: '应急通知配置',
        label: '应急通知配置',
        route: '/informconfig',
      },
      {
        key: '280300',
        // icon: menuIconDot,
        title: '客户应急消息群发',
        label: '客户应急消息群发',
        route: '/massmsg',
      },
    ],
  },
  {
    key: '430000',
    icon: <SettingOutlined />,
    label: '配置管理',
    children: [
      // {
      //   key: '430200',
      //   // icon: menuIconDot,
      //   title: '应急通知配置',
      //   label: '应急通知配置',
      //   route: '/informconfig',
      // },
      {
        key: '430300',
        // icon: menuIconDot,
        title: '自动预警配置',
        label: '自动预警配置',
        route: '/autoconfig',
      },
      {
        key: '430400',
        // icon: menuIconDot,
        title: '机构开业配置',
        label: '机构开业配置',
        route: '/unticonfig',
      },
    ],
  },
  {
    key: '630000',
    icon: <UnlockOutlined />,
    label: '权限管理',
    children: [
      {
        key: '630100',
        title: '用户管理',
        label: '用户管理',
        route: '/userConfig',
      },
      // {
      //   key: '630200',
      //   title: '人员权限配置',
      //   label: '人员权限配置',
      //   route: '/userConfig',
      // },
      {
        key: '630200',
        title: '角色权限配置',
        label: '角色权限配置',
        route: '/role',
      },
    ],
  },
]

const getFullGrantedMenu = ({ menuList = fullMenuTree, permissions }) => {
  if (!permissions || !Array.isArray(permissions)) {
    return []
  }

  let grantedList = []

  for (let i = 0; i < menuList.length; i += 1) {
    const menuItem = menuList[i]

    // 当前 item 为页面
    if (permissions?.includes(menuItem.key) || menuItem.children) {
      let grantedSubmenuList
      if (menuItem.children) {
        grantedSubmenuList = getFullGrantedMenu({ menuList: menuItem.children, permissions })
      }

      grantedList.push({ ...menuItem, children: grantedSubmenuList })
    }
  }

  return grantedList
}

const getGrantedMenu = ({ permissions }) => {
  let grantedList = []

  const fullGrantedList = getFullGrantedMenu({ menuList: fullMenuTree, permissions })
  for (let i = 0; i < fullGrantedList.length; i += 1) {
    let menuItem = fullGrantedList[i]
    if (menuItem.children) {
      if (menuItem.children.length > 0) {
        grantedList.push(menuItem)
      }
    } else {
      grantedList.push(menuItem)
    }
  }

  return grantedList
}

function getMenuItem(menuTree = [], routePath) {
  for (const item of menuTree) {
    if (item.route === routePath) return item

    if (item.children) {
      const menuItem = getMenuItem(item.children, routePath)
      if (Object.keys(menuItem).length) return menuItem
    }
  }
  return {}
}

export { getGrantedMenu, getMenuItem }
